/*
 * @Description: 
 * @Author: fhw
 * @Date: 2022-12-05 15:03:51
 */
import { Injectable, UseFilters } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { clone, isEmpty, isNil } from 'ramda';
import { ChatUser } from 'src/chat_user/entities/chat_user.entity';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { getPagination, throwHttp } from 'src/utlis';
import { Repository, Connection, IsNull } from 'typeorm';
import { CreateleaveAMessageDto, GetUserPageDto } from './dto/message-board.dto';
import { MessageBoard } from './entities/message-board.entity';

@Injectable()
export class MessageBoardService {
    constructor(
        @InjectRepository(MessageBoard) private messageBoardRepository: Repository<MessageBoard>,
    ) { }
    @UseFilters(new HttpExceptionFilter())
    async getMessageBoardPage(params: GetUserPageDto): Promise<PageResponse> {
        try {
            const { pageSize: size, pageNum: num, } = params
            const { pageSize, pageNum } = getPagination({ pageSize: size, pageNum: num })
            const [data, total] = await this.messageBoardRepository.findAndCount({
                where: { messageBoardId: IsNull() },
                skip: pageNum * pageSize,
                take: pageSize,
                order: { createDate: 'DESC' },
                cache: true,
                relations: ['twoMessageBoard', 'user']
            })
            return {
                data,
                total,
                pageSize,
                pageNum: pageNum * pageSize + 1,
            }
        } catch (error) {
            throwHttp(error.message)
        }
    }
    @UseFilters(new HttpExceptionFilter())
    async createleaveAMessage(body: CreateleaveAMessageDto, { user, toUser }: { user: ChatUser; toUser?: ChatUser }) {
        try {
            const { userId, content, messageBoardId, toUserId } = body
            const messageBoard = new MessageBoard()
            messageBoard.user = user
            messageBoard.userId = userId
            messageBoard.content = content
            const messageBoardInfo = await this.getMessageBoardInfo(messageBoardId)
            if (!isNil(messageBoardId) && !isEmpty(messageBoardId)) {
                messageBoard.messageBoardId = messageBoardId
                messageBoard.messageBoard = messageBoardInfo
                messageBoard.toUser = toUser
                messageBoard.toUserId = toUserId
            }
            const result = await this.messageBoardRepository.manager.save(messageBoard)
            return result
        } catch (error) {
            throwHttp(error.message)
        }
    }
    // 获取用户信息
    @UseFilters(new HttpExceptionFilter())
    async getMessageBoardInfo(id: number): Promise<MessageBoard> {
        try {
            const data = await this.messageBoardRepository.findOne({
                where: { id, },
            })
            if (isNil(data)) {
                throwHttp('留言不存在')
            }
            return data
        } catch (error) {
            throwHttp(error.message)
        }
    }
}
